home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / terms / kermit / b / ckonet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-30  |  6.8 KB  |  353 lines

  1. char *ckonetv = "OS/2 Network support, 5A(009) 06 May 93";
  2.  
  3. /*  C K O N E T  --  Network support  */
  4. /*
  5.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  6.   York.  Permission is granted to any individual or institution to use this
  7.   software as long as it is not sold for profit.  This copyright notice must be
  8.   retained.  This software may not be included in commercial products without
  9.   written permission of Columbia University.
  10. */
  11.  
  12. /* Currently supported network services:
  13.  
  14.  - DECnet LAT
  15.  - TCP/IP Telnet
  16.  
  17.  */
  18.  
  19. #include "ckcdeb.h"
  20. #include "ckcker.h"
  21. #include "ckcnet.h"
  22. #include "ckuusr.h"
  23.  
  24. #include <io.h>
  25. #include <fcntl.h>
  26. #include <string.h>
  27.  
  28. extern int ttnproto, tt_type;
  29. extern char *tn_term;
  30.  
  31. #ifndef NETCONN
  32. /*
  33.   Network support not defined.
  34.   Dummy functions here in case #ifdef's forgotten elsewhere.
  35. */
  36. int                    /* Open network connection */
  37. os2_netopen(name, lcl, nett) char *name; int *lcl, nett; {
  38.     return(-1);
  39. }
  40. int                    /* Close network connection */
  41. os2_netclos() {
  42.     return(-1);
  43. }
  44. int                    /* Check network input buffer */
  45. os2_nettchk() {
  46.     return(-1);
  47. }
  48. int                    /* Flush network input buffer */
  49. os2_netflui() {
  50.     return(-1);
  51. }
  52. int                    /* Send network BREAK */
  53. os2_netbreak() {
  54.     return(-1);
  55. }
  56. int                    /* Input character from network */
  57. os2_netinc(timo) int timo; {
  58. }
  59. int                    /* Output character to network */
  60. os2_nettoc(c) int c; {
  61.     return(-1);
  62. }
  63. int
  64. os2_nettol(s,n) char *s; int n; {
  65.     return(-1);
  66. }
  67.  
  68. #else /* NETCONN is defined (rest of this module...) */
  69.  
  70. #ifndef __32BIT__
  71. #define far _far
  72. #define near _near
  73. #define pascal _pascal
  74. #endif
  75. #define    INCL_NOPM
  76. #define    INCL_DOSPROCESS
  77. #define    INCL_DOSMODULEMGR
  78. #include <os2.h>
  79.  
  80. #ifdef DECNET
  81. #ifndef __32BIT__
  82. #define _Far16 _far
  83. #define _Pascal _pascal
  84. #define _Seg16
  85. #endif
  86. #include "ckolat.h"
  87. typedef short (* _Far16 _Pascal LATTYPE)(struct lat_cb * _Seg16);
  88. LATTYPE LATENTRY;
  89. static struct lat_cb lcb;
  90. #endif
  91.  
  92. extern int duplex, debses, seslog, ttyfd, quiet; /* External variables */
  93. extern int nettype;
  94.  
  95. /*  N E T O P E N  --  Open a network connection.  */
  96. /*  Returns 0 on success, -1 on failure.  */
  97.  
  98. /*
  99.   Calling conventions same as ttopen(), except third argument is network
  100.   type rather than modem type.  Designed to be called from within ttopen.
  101. */
  102.  
  103. int
  104. os2_netopen(name, lcl, nett) char *name; int *lcl, nett; {
  105.  
  106.     int rc = -1;
  107.  
  108. #ifdef TCPSOCKET
  109.     if ( nettype == NET_TCPB ) return netopen(name, lcl, nett);
  110. #endif
  111.  
  112. #ifdef DECNET
  113.     if ( LATENTRY == NULL )
  114.       return -1;
  115.  
  116.     ttnproto = NP_LAT;
  117.  
  118.     printf("Trying connection to %s ... ", name);
  119.  
  120.     lcb.LatFunction = START_SESSION;
  121.     lcb.BufferSize = strlen(name);
  122.     lcb.BufferPtr = (void * _Seg16) name;
  123.  
  124.     LATENTRY(&lcb);
  125.  
  126.     ttyfd = lcb.SessionHandle;
  127.     printf(lcb.LatStatus ? "failed.\n" : "OK.\n");
  128.  
  129.     rc = (lcb.LatStatus == 0) ? 0 : -1;
  130. #endif
  131.  
  132.     return rc;
  133. }
  134.  
  135. /*  N E T C L O S  --  Close current network connection.  */
  136.  
  137. int
  138. os2_netclos() {
  139.     int x = 0;
  140.  
  141.     if (ttyfd < 0)            /* Was open? */
  142.       return(0);            /* Wasn't. */
  143.  
  144. #ifdef TCPSOCKET
  145.     if ( nettype == NET_TCPB ) return netclos();
  146. #endif
  147.     
  148. #ifdef DECNET
  149.     if (ttyfd > -1)            /* Was. */
  150.     {
  151.       lcb.LatFunction = STOP_SESSION;
  152.       lcb.SessionHandle = ttyfd;
  153.  
  154.       LATENTRY(&lcb);
  155.  
  156.       x = (lcb.LatStatus == 0) ? 0 : -1;
  157.     }
  158. #endif
  159.  
  160.     ttyfd = -1;                /* Mark it as closed. */
  161.  
  162.     return(x);
  163. }
  164.  
  165. /*  N E T T C H K  --  Check if network up, and how many bytes can be read */
  166. /*
  167.   Returns number of bytes waiting, or -1 if connection has been dropped.
  168. */
  169. int                    /* Check how many bytes are ready */
  170. os2_nettchk() {                /* for reading from network */
  171. #ifdef TCPSOCKET
  172.     if ( nettype == NET_TCPB ) return nettchk();
  173. #endif
  174.     return(0);
  175. }
  176.  
  177. /*  N E T T I N C --  Input character from network */
  178.  
  179. int
  180. os2_netinc(timo) int timo; {
  181.  
  182.     static char buffer[256];
  183.     static int size = 0, pos = 0;
  184.     int chr = -1;
  185.  
  186. #ifdef TCPSOCKET
  187.     if ( nettype == NET_TCPB ) {
  188.       if ( (chr = netinc(timo)) != -1 )
  189.     return chr;
  190.       else
  191.     return (nettchk() == -1) ? -2 : -1;
  192.     }
  193. #endif
  194.     
  195. #ifdef DECNET
  196.     if ( pos < size )
  197.       return buffer[pos++];
  198.  
  199.     lcb.LatFunction = GET_CHAR_BLK;
  200.     lcb.SessionHandle = ttyfd;
  201.     lcb.BufferSize = sizeof(buffer);
  202.     lcb.BufferPtr = (void * _Seg16) buffer;
  203.     lcb.WaitTime = timo < 0 ? 10L * -timo : 1000L * timo;
  204.  
  205.     LATENTRY(&lcb);
  206.  
  207.     if ( (lcb.SessionStatus & 0xFF) == SS_Stopped )
  208.       return -2;
  209.     if ( lcb.LatStatus )
  210.       return -1;
  211.  
  212.     pos = 0;
  213.     size = lcb.BufferSize;
  214.  
  215.     chr = buffer[pos++];
  216. #endif
  217.  
  218.     return chr;
  219. }
  220.  
  221. /*  N E T T O C  --   Output character to network */
  222. /*
  223.   Call with character to be transmitted.
  224.   Returns 0 if transmission was successful, or
  225.   -1 upon i/o error, or -2 if called improperly.
  226. */
  227. int
  228. os2_nettoc(c) int c; {
  229.  
  230.     int rc = -1;
  231.  
  232. #ifdef TCPSOCKET
  233.     if ( nettype == NET_TCPB ) return nettoc((char) c);
  234. #endif
  235.     
  236. #ifdef DECNET
  237.     lcb.LatFunction = SEND_CHAR;
  238.     lcb.SessionHandle = ttyfd;
  239.     lcb.CharByte = c;
  240.  
  241.     LATENTRY(&lcb);
  242.  
  243.     rc = (lcb.LatStatus == 0) ? 0 : -1;
  244. #endif
  245.  
  246.     return rc;
  247. }
  248.  
  249. /*  N E T T O L  --  Output a string of bytes to the network  */
  250. /*
  251.   Call with s = pointer to string, n = length.
  252.   Returns number of bytes actuall written on success, or
  253.   -1 on i/o error, -2 if called improperly.
  254. */
  255. int
  256. os2_nettol(s,n) char *s; int n; {
  257.  
  258.     int b;
  259.  
  260. #ifdef TCPSOCKET
  261.     if ( nettype == NET_TCPB ) return nettol(s, n);
  262. #endif
  263.     
  264.     for ( b = 0; b < n; b++, s++ )
  265.       if ( nettoc(*s) )
  266.         break;
  267.  
  268.     return(b);
  269. }
  270.  
  271. /*  N E T F L U I  --  Flush network input buffer  */
  272.  
  273. int
  274. os2_netflui() {
  275. #ifdef TCPSOCKET
  276.     if ( nettype == NET_TCPB ) return netflui();
  277. #endif
  278.     return(0);
  279. }
  280.  
  281. /* Send network BREAK */
  282. /*
  283.   Returns -1 on error, 0 if nothing happens, 1 if BREAK sent successfully.
  284. */
  285. int
  286. os2_netbreak() {
  287.  
  288.     int rc = -1;
  289.  
  290. #ifdef TCPSOCKET
  291.     if ( nettype == NET_TCPB ) return netbreak();
  292. #endif
  293.     
  294. #ifdef DECNET
  295.     lcb.LatFunction = SEND_BREAK;
  296.     lcb.SessionHandle = ttyfd;
  297.  
  298.     LATENTRY(&lcb);
  299.  
  300.     rc = (lcb.LatStatus == 0) ? 0 : -1;
  301. #endif
  302.  
  303.     return rc;
  304. }
  305.  
  306. /*
  307.  * what follows is all dynamic link stuff to let the same executable
  308.  * run on machines with and without networking software
  309.  */
  310.  
  311. typedef (*function)();
  312.  
  313. static function LoadProcedure(HMODULE module, char *procedure)
  314. {
  315.     PFN entry;
  316.  
  317. #ifdef __32BIT__
  318.     if ( DosQueryProcAddr(module, 0, procedure, (PFN *) &entry) )
  319. #else
  320.     if ( DosGetProcAddr(module, procedure, &entry) )
  321. #endif
  322.         return NULL;
  323.     else
  324.         return (function) entry;
  325. }
  326.  
  327. netinit() {
  328.  
  329.     char fail[256];
  330.     HMODULE library;
  331.  
  332. #ifdef DECNET
  333.     if ( DosLoadModule(fail, sizeof(fail), "LATCALLS", &library) == 0 )
  334.       LATENTRY = (LATTYPE) LoadProcedure(library, "LATENTRY");
  335. #endif
  336.  
  337. #ifdef TCPSOCKET
  338. #ifdef TCPIBM
  339.     sock_init();
  340. #endif
  341.  
  342.     if (tn_term == NULL)
  343.       if (tt_type == TT_VT52)
  344.     tn_term = strdup("vt52");
  345.       else if (tt_type == TT_VT102)
  346.     tn_term = strdup("vt102");
  347. #endif
  348.  
  349.     return 1;
  350. }
  351.  
  352. #endif /* NETCONN */
  353.